<!--
 * @Author: your name
 * @Date: 2021-05-17 14:45:07
 * @LastEditTime: 2021-05-18 10:56:49
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \solaxcloud\src\views\test\print\partNoSet.vue
-->
<template>
    <div class="partNoSet-main-box">
        <div class="search-box">
            <div class="search-box-sub" >
                <span class="lable-text">{{$t('version.versionAdd.partNumber')}}</span>
                <el-select v-model="partNo" filterable :placeholder="$t('pleaseSelect')" :clearable='true' >
                    <el-option
                    v-for="item in textDataList"
                    :key="item.code"
                    :label="item.code"
                    :value="item.code">
                    </el-option>
                </el-select>
            </div>
            <div class="search-box-sub" >
                <span class="lable-text">机型</span>
                <el-select v-model="inverterType" filterable :placeholder="$t('pleaseSelect')" :clearable='true'>
                    <el-option
                    v-for="item in inverterTypeList"
                    :key="item.value"
                    :label="item.code"
                    :value="item.value">
                    </el-option>
                </el-select>
            </div>
            <div class="search-box-sub">
                <el-button type="primary" @click='query'>查询</el-button>
                <a href="javascript:;" style="margin-left:10px;color:orange;" @click='$emit("update:pageFlag",false)'><<返回打印页</a>
            </div>
        </div>
        <div class="button-box">
            <el-button   type="primary" @click='addVisible = true'  class="marginR10" >新增</el-button>
            <el-popconfirm
            :confirm-button-text="$t('sure')"
            :cancel-button-text="$t('cancel')"
            :title="$t('index.delete_item_confirm')"
            @confirm='deleteRowBatch()'
            >
                <el-button class="marginR10" type="" slot="reference"  >批量删除</el-button>
            </el-popconfirm>
        </div>
        <baseTable v-loading="loading"  row-key='sid' :data="dataRows" border  stripe :column='column' :select='true' @selection-change="handleSelectionChange($event)">
            <template #option='{data}'>
                <div class="option-box">
                    <el-button size='small' type="primary" icon="el-icon-edit"  @click='showEditModal(data)' class="marginR10" :title="$t('edit')"></el-button>
                    <el-popconfirm
                        :confirm-button-text="$t('sure')"
                        :cancel-button-text="$t('cancel')"
                        :title="$t('index.delete_item_confirm')"
                        @confirm='deleteRow(data.sid)'
                    >
                        <el-button class="marginR10" type="" slot="reference" icon='el-icon-delete' :title="$t('delete')"></el-button>
                    </el-popconfirm>
                </div>
            </template>
        </baseTable>
        <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="current"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
        </el-pagination>
        <addSet :addVisible.sync='addVisible' :textDataList='textDataList' :inverterTypeList='inverterTypeList' @uploadData='prtsettingGetPage' v-if='addVisible' :PartNoAdd='PartNoAdd'></addSet>
        <editSet :editVisible.sync='editVisible' :textDataList='textDataList' :inverterTypeList='inverterTypeList' @uploadData='prtsettingGetPage' :editDatRow='editDatRow'></editSet>
    </div>
</template>

<script>
import baseTable from '../../../components/common/baseTable'
import addSet from './addSet'
import editSet from './editSet'
export default {
    components: {
        baseTable,
        addSet,
        editSet,
    },
    props: {
        pageFlag:{
            type:Boolean,
            default:false
        }
    },
    data() {
        return {
            partNo:'',
            inverterType:'',
            PartNoAdd:'',
            current:1,
            size:10,
            total:1,
            inverterTypeList:[],
            textDataList:[],
            deviceTypeList:[],
            loading:false,
            addVisible:false,
            editVisible:false,
            editDatRow:{},
            column:[{
                title: '料号',
                value: 'partNo',
                slot: 'partNo',
            },{
                title: '机型',
                value: 'inverterType',
                slot: 'inverterType',
            },{
                title: this.$t('control'),
                value: 'option',
                slot: 'option',
            }],
            dataRows:[],
        };
    },
    computed: {
        printPartNo(){
            return this.$store.state.sysStore.printPartNo
        }
    },
    watch: {
        printPartNo:{
            handler(nv,ov){
                if(nv){
                    this.addVisible = true
                    this.PartNoAdd = nv
                }
            },
            immediate:true
        },
        pageFlag:{
            handler(nv,ov){
                // if(nv){
                    console.log('partNoSet');
                // }
            },
            deep:true,
            immediate:true
        }
    },
    created() {

    },
    mounted() {
        this.getDictionaryBatch()
        this.prtsettingGetPage()
    },
    methods: {
        handleSelectionChange(val){
          this.selectDataList=val
        },
        async deleteRow(id){
            let res = await this.API.prtsettingDeleteBatch({
                ids:id
            })
            if(this.comm.showMsg(res.success)){
                this.prtsettingGetPage()
            }
        },
        async deleteRowBatch(){
            if(this.selectDataList.length == 0){
                this.comm.showMsg(false,this.$t('index.delete_select'))
                return
            }
            let arr = []
            this.selectDataList.forEach(item=>{
                arr.push(item.sid)
            })
            let res = await this.API.prtsettingDeleteBatch({
                ids:arr
            })
            if(this.comm.showMsg(res.success)){
                this.prtsettingGetPage()
            }
        },
        showEditModal(data){
            this.editVisible = true
            this.editDatRow = data
        },
        async prtsettingGetPage(){
            this.loading = true
            let res = await this.API.prtsettingGetPage({
                partNo:this.partNo,
                type:this.inverterType,
                inverterType:this.inverterType?this.comm.returnCode(this.inverterTypeList,this.inverterType):'',
                size:this.size,
                current:this.current
            })
            this.loading = false
            this.total = res.total
            this.dataRows = res.rows
        },
        addModal(){
            
        },
        async getDictionaryBatch(){
            let res = await this.API.getDictionaryBatch({
                dictCodes:'WIFI_PART_NUM,INVERTER_TYPES,PRT_DEVICE_TYPES',
                lang:localStorage.getItem('lan') || 'zh_CN'
            })
            this.textDataList = res.WIFI_PART_NUM || []
            this.inverterTypeList = res.INVERTER_TYPES || []
            this.deviceTypeList = res.PRT_DEVICE_TYPES || []
        },
        query(){
            this.current = 1
            this.prtsettingGetPage()
        },
        handleSizeChange(val) {
            this.size = val
            this.current = 1
            this.prtsettingGetPage()
        },
        handleCurrentChange(val) { 
            this.current = val
            this.prtsettingGetPage()
        },
    },
};
</script>

<style  lang="less" scoped>

</style>

